<template>
	<view>
		<view class="body">
			<view class="uni-media-list">
				<image class="uni-media-list-logo" :src="info.thumb"></image>
				<view class="uni-media-list-body">
					<view class="uni-media-list-text-top">{{ info.title }}</view>
					<view class="uni-media-list-text-bottom">
						<text>{{ info.author }}</text>
						<text>{{ info.price }}</text>
					</view>
				</view>
			</view>
		</view>
		
		<view class="footer">
			<view class="left">
				<view class="text">实际支付: <text>{{ info.price }}</text></view>
				<view class="text">当前余额: <text>{{ user.balance }}</text></view>
			</view>
			<view class="right">
				<button class="buy" type="primary" @click="createOrder(info)" v-if="user.balance > info.price">提交订单</button>
				<button class="buy" type="primary" @click="goRecharge()" v-else>去充值</button>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				user: null,
				info: {}
			}
		},
		onLoad(event) {
			const payload = event.payload;
			try {
				this.info = JSON.parse(decodeURIComponent(payload));
			} catch (error) {
				this.info = JSON.parse(payload);
			}
			this.user = uni.getStorageSync('USER')
		},
		methods: {
			goRecharge(id) {
				uni.navigateTo({
					url: '../user/recharge'
				})
			},
			createOrder(e) {
				this.$store.dispatch('request', {
					url: "course_order",
					method: "POST", 
					data: {
						"course_id": e.id,
						"price": e.price
					},
				}).then((res) => {
					console.log(res);
				}).catch((error) => {
				 	console.log(error)
				})
			}
		}
	}
</script>

<style lang="scss">
	.uni-media-list-logo {
		width: 240rpx;
		height: 140rpx;
	}
	
	.uni-media-list-body {
		height: auto;
		justify-content: space-around;
	}
	
	.uni-media-list-text-top {
		height: 74rpx;
		font-size: 28rpx;
		overflow: hidden;
	}
	
	.uni-media-list-text-bottom {
		display: flex;
		flex-direction: row;
		justify-content: space-between;
	}
	
	.footer {
		position: fixed;
		bottom: 30upx;
		width: 100%;
		display: flex;
		align-items: flex-end;
		.left {
			width: 300upx;
			padding-left: 30upx;
		}
		
		.right {
			position: fixed;
			right: 0;
			width: 300upx;
			.buy {
				border-radius: 40upx;
				width: 100%;
			}
		}
	}
</style>
